﻿// <copyright file="EnOceanService.cs" company="Hallvard Korsgaard">
//  Microsoft Public License (Ms-PL). See license details here: http://enoceannet.codeplex.com/license
// </copyright>
// <author>Hallvard Korsgaard</author>

namespace EnOceanService
{
  using System;
  using System.Diagnostics;
  using System.ServiceProcess;

  /// <summary>
  /// The Windows service.
  /// </summary>
  public partial class EnOceanService : ServiceBase
  {
    /// <summary>
    /// Initializes a new instance of the EnOceanService class.
    /// </summary>
    public EnOceanService()
    {
      InitializeComponent();
    }

    /// <summary>
    /// Called by the service runtime to start the service.
    /// </summary>
    /// <param name="args">Serivce arguments.</param>
    protected override void OnStart(string[] args)
    {
      try
      {
        StcEthernetBroker.EventLog = this.EventLog;
        ReceiverBroker.EventLog = this.EventLog;

        StcEthernetBroker.Start();
        ReceiverBroker.Start();
        TransmitterService.Start();
      }
      catch (Exception e)
      {
        EventLog.WriteEntry(e.ToString(), EventLogEntryType.Error);
      }
    }

    /// <summary>
    /// Called by the service runtime to stop the service.
    /// </summary>
    protected override void OnStop()
    {
      try
      {
        TransmitterService.Stop();
        ReceiverBroker.Stop();
        StcEthernetBroker.Stop();
      }
      catch (Exception e)
      {
        EventLog.WriteEntry(e.ToString(), EventLogEntryType.Error);
      }
    }
  }
}
